package com.jmu.aslongastoday.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jmu.aslongastoday.common.Result;
import com.jmu.aslongastoday.domain.LogBrowsing;
import com.jmu.aslongastoday.dto.RecordBrowsingDto;
import com.jmu.aslongastoday.service.LogBrowsingService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("log")
public class RecordBrowsingController {
    @Resource
    LogBrowsingService logBrowsingService;

    @GetMapping("get")
    public Result get(String userName){
        LambdaQueryWrapper<LogBrowsing> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LogBrowsing::getUserName,userName);
        queryWrapper.orderByDesc(LogBrowsing::getLogId);
        List<LogBrowsing> list = logBrowsingService.list(queryWrapper);

        return Result.success(list);
    }

    @PostMapping("add")
    public Result add(@RequestBody RecordBrowsingDto dto){
        LogBrowsing logBrowsing = new LogBrowsing();
        logBrowsing.setLogName(dto.getLogName());
        logBrowsing.setLogTime(new Date());
        logBrowsing.setUserName(dto.getUserName());
        logBrowsingService.save(logBrowsing);
        return Result.success("");
    }
}
